/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.servicecomb.fence.resource;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.apache.servicecomb.fence.api.resource.HandlerAuthService;
import org.apache.servicecomb.fence.order.CashRepayApply;
import org.apache.servicecomb.fence.order.CashRepayApplyMapper;
import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.json.simple.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

@RestSchema(schemaId = "HandlerAuthEndpoint", schemaInterface = HandlerAuthService.class)
public class HandlerAuthEndpoint implements HandlerAuthService {

  @Autowired(required = false)
  private CashRepayApplyMapper cashRepayApplyMapper;


  @Override
  public String adminSayHello(String memberId) {
    return JSONArray.toJSONString(cashRepayApplyMapper.selectCashRepayByMemberId(memberId));
  }

  @Override
  public String guestSayHello(String name) {
    return name;
  }

  @Override
  public String guestOrAdminSayHello(String name) {
    return name;
  }

  @Override
  public String everyoneSayHello(String name) {
    return name;
  }

  @Override
  public String insertShardingCashApply() {
    CashRepayApply cashRepayApply = new CashRepayApply();
    List<String> memeberList = new ArrayList<>(Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"));

    Random rand = new Random();
    int randomNumber = rand.nextInt(100);

    Snowflake snowflake = new Snowflake(null, 0, 0, false, Snowflake.DEFAULT_TIME_OFFSET, 4);

    long snowflakeId = snowflake.nextId();

    cashRepayApply.setId(snowflakeId);
    cashRepayApply.setRepayNo(String.valueOf(System.currentTimeMillis()));
    cashRepayApply.setMemberId(memeberList.get(randomNumber % memeberList.size()));
    cashRepayApplyMapper.insertShardingCashRepayApply(cashRepayApply);
    return "success";
  }
}
